using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MM.Business;
using MM.Entities;

namespace MM
{
    public partial class frmHienthiPhieuNK : Form
    {
        string _matau = "";
        string _nguoitao = "";
        string _sConn = "";

        
        public frmHienthiPhieuNK(string sConn,string MaPhieuNhapHang)
        {
            InitializeComponent();

            _sConn = sConn;
            _matau = frmDangNhap.objTau._tenTau2KiTu;

            if (MaPhieuNhapHang == "")
            {
                this.HienthiPHIEU_NHAP_HANG_MAX(_matau);
                MaPhieuNhapHang = this.txtSoPhieuGR.Text;
            }
            else
                this.HienthiPHIEU_NHAP_HANG(MaPhieuNhapHang);
            this.HienthiCHI_TIET_PNH(this.txtSoPhieuGR.Text);
            this.HienthiCHI_TIET_GT_ND(this.txtSoPhieuGR.Text);
            
        }

        public frmHienthiPhieuNK(string sConn)
        {
            InitializeComponent();

            _sConn = sConn;
            _matau = frmDangNhap.objTau._tenTau2KiTu;
           
            this.HienthiPHIEU_NHAP_HANG_MAX(_matau);   
                     
            this.HienthiCHI_TIET_PNH(this.txtSoPhieuGR.Text);
            this.HienthiCHI_TIET_GT_ND(this.txtSoPhieuGR.Text);

        }


        private void HienthiPHIEU_NHAP_HANG_MAX(string matau)
        {
            clsPHIEU_NHAP_HANG pnhCtrl = new clsPHIEU_NHAP_HANG(_sConn);
            List<PHIEU_NHAP_HANG> lst = new List<PHIEU_NHAP_HANG>();
            lst = pnhCtrl.usp_SelectPHIEU_NHAP_HANG_MAX(matau);
            if (lst.Count > 0)
            {
                this.txtSoPhieuGR.Text = lst[0].MaPhieuNhapHang;
                this.txtSoPhieuPO.Text = lst[0].POThamChieuSAP;
                this.txtNgayThucHien.Text = lst[0].NgayNhap.Day.ToString() + "/" + lst[0].NgayNhap.Month.ToString() + "/" + lst[0].NgayNhap.Year.ToString();
                this.txtNguoiThucHien.Text = lst[0].NguoiNhap;

                if (lst[0].NgayExport.HasValue)
                {
                    //DateTime dE = (DateTime)lst[0].NgayExport;
                    //this.txtNgayDuyet.Text = dE.Day.ToString() + "/" + dE.Month.ToString() + "/" + dE.Year.ToString();                  
                    this.chkDaduyet.Checked = true;
                }
                else
                {
                    //this.txtNgayDuyet.Text = "";    
                    this.chkDaduyet.Checked = false;
                }

                if (lst[0].TinhTrang == 1)
                {
                    this.chkXoa.Checked = true;
                }
                else
                    this.chkXoa.Checked = false;
            }

        }
        private void HienthiPHIEU_NHAP_HANG(string MaPhieuNhapHang)
        {
            clsPHIEU_NHAP_HANG pnhCtrl = new clsPHIEU_NHAP_HANG(_sConn);
            List<PHIEU_NHAP_HANG> lst = new List<PHIEU_NHAP_HANG>();
            lst = pnhCtrl.usp_SelectPHIEU_NHAP_HANG(MaPhieuNhapHang);
            if (lst.Count > 0)
            {
                this.txtSoPhieuGR.Text = lst[0].MaPhieuNhapHang;
                this.txtSoPhieuPO.Text = lst[0].POThamChieuSAP;
                this.txtNgayThucHien.Text = lst[0].NgayNhap.Day.ToString() + "/" + lst[0].NgayNhap.Month.ToString() + "/" + lst[0].NgayNhap.Year.ToString();
                this.txtNguoiThucHien.Text = lst[0].NguoiNhap;

                if (lst[0].NgayExport.HasValue)
                {
                    //DateTime dE = (DateTime)lst[0].NgayExport;
                    //this.txtNgayDuyet.Text = dE.Day.ToString() + "/" + dE.Month.ToString() + "/" + dE.Year.ToString();                  
                    this.chkDaduyet.Checked = true;
                }
                else
                {
                    //this.txtNgayDuyet.Text = "";    
                    this.chkDaduyet.Checked = false;
                }

                if (lst[0].TinhTrang == 1)
                {
                    this.chkXoa.Checked = true;
                }
                else
                    this.chkXoa.Checked = false;
            }
            else
            {
                this.txtSoPhieuGR.Text = "";
                this.txtSoPhieuPO.Text = "";
                this.txtNgayThucHien.Text = "";
                this.txtNguoiThucHien.Text = "";
            }

        }
        private void HienthiCHI_TIET_PNH(string MaPhieuNhapHang)
        {
            clsCHI_TIET_PNH ctCtrl = new clsCHI_TIET_PNH(_sConn);
            List<CHI_TIET_PNH> lst = new List<CHI_TIET_PNH>();
            lst = ctCtrl.usp_SelectCHI_TIET_PNH_ByMaPhieuNhapHang(MaPhieuNhapHang);
            this.gridControl1.DataSource = lst;
        }
        private void HienthiCHI_TIET_GT_ND(string MaPhieuNhapHang)
        {
            try
            {
                if (MaPhieuNhapHang != "")
                {
                    clsCHI_TIET_ND_GT gtCtrl = new clsCHI_TIET_ND_GT(_sConn);
                    List<CHI_TIET_ND_GT> lstGT = new List<CHI_TIET_ND_GT>();
                    lstGT = gtCtrl.usp_SelectCHI_TIET_ND_GT_byMaGiaoDich(MaPhieuNhapHang);

                    this.gridControl2.DataSource = lstGT;
                }
                else
                    this.gridControl2.DataSource = null;
            }
            catch { }

        }

        private void frmHienthiPhieuNK_Paint(object sender, PaintEventArgs e)
        {
           // Common.PaintGradientColor(Color.LightYellow, Color.LightSkyBlue, System.Drawing.Drawing2D.LinearGradientMode.Vertical, this.Width, this.Height, this);            
        }

        private void txtSoPhieuGR_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string maphieunhaphang = this.txtSoPhieuGR.Text.Trim();
                this.HienthiPHIEU_NHAP_HANG(maphieunhaphang);
                this.HienthiCHI_TIET_PNH(maphieunhaphang);
                this.HienthiCHI_TIET_GT_ND(maphieunhaphang);
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {
            frmTimPhieuNK f = new frmTimPhieuNK(_sConn);
            f.chonPNK += new frmTimPhieuNK.ChonPhieuNhapKho(GetPhieuNhapKho);
            f.Show(this);
        }

        private void GetPhieuNhapKho(object sender, ChonPhieuNKEventArgs e)
        {
            string mapnk = e.MaPhieuNhapKho;

            this.HienthiPHIEU_NHAP_HANG(mapnk);
            this.HienthiCHI_TIET_PNH(mapnk);
            this.HienthiCHI_TIET_GT_ND(mapnk);
        }

        private void frmHienthiPhieuNK_Load(object sender, EventArgs e)
        {
            Common.SetColor(this);
        }
    }
}